package GUI;
import CTRLayer.*;

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.event.ActionListener;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.JCheckBox;
import javax.swing.JList;
import javax.swing.AbstractListModel;
import javax.swing.ListSelectionModel;

import java.awt.event.ActionEvent;

import javax.swing.JComboBox;
import javax.swing.DefaultComboBoxModel;

import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JScrollPane;

public class Clothing extends JFrame {

	private JPanel contentPane;
	private JTextField txtID;
	private JTextField txtName;
	private JTextField txtSalesPrice;
	private JTextField txtCofO;
	private JTextField txtSupp;
	private JButton btnAddNew;
	private JButton btnShowInformation;
	private JButton btnDelete;
	private JButton btnBackToMain;
	private JLabel lblSize;
	private JLabel lblColour;
	private JTextField txtColour;
	private JLabel lblType;
	private JTextField txtType;
	private JLabel lblFabric;
	private JTextField txtFabric;
	private JLabel lblCalibre;
	private JTextField txtCalibre;
	private AddProduct addprod;
	private JComboBox<String> comboBox;
	private Controller ctr;
	private JTable tableProds;
	
	/**
	 * Create the frame.
	 */
	public Clothing() {
		
		ctr = new Controller();
		
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 550, 450);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		setTitle("Clothing");
		contentPane.setLayout(null);
		
		JLabel lblId = new JLabel("ID:");
		lblId.setBounds(10, 11, 100, 14);
		contentPane.add(lblId);
		
		JLabel lblName = new JLabel("Name:");
		lblName.setBounds(10, 36, 100, 14);
		contentPane.add(lblName);
		
		JLabel lblSalesPrice = new JLabel("Sales price:");
		lblSalesPrice.setBounds(10, 61, 100, 14);
		contentPane.add(lblSalesPrice);
		
		JLabel lblCountryOfOrigin = new JLabel("Country of origin:");
		lblCountryOfOrigin.setBounds(10, 86, 100, 14);
		contentPane.add(lblCountryOfOrigin);
		
		JLabel lblSupplier = new JLabel("Supplier:");
		lblSupplier.setBounds(10, 111, 100, 14);
		contentPane.add(lblSupplier);
		
		txtID = new JTextField();
		txtID.setBounds(120, 8, 86, 20);
		contentPane.add(txtID);
		txtID.setColumns(10);
		
		txtName = new JTextField();
		txtName.setBounds(120, 33, 86, 20);
		contentPane.add(txtName);
		txtName.setColumns(10);
		
		txtSalesPrice = new JTextField();
		txtSalesPrice.setBounds(120, 58, 86, 20);
		contentPane.add(txtSalesPrice);
		txtSalesPrice.setColumns(10);
		
		txtCofO = new JTextField();
		txtCofO.setBounds(120, 83, 86, 20);
		contentPane.add(txtCofO);
		txtCofO.setColumns(10);
		
		txtSupp = new JTextField();
		txtSupp.setBounds(120, 108, 86, 20);
		contentPane.add(txtSupp);
		txtSupp.setColumns(10);
		

		JButton btnSearch = new JButton("Search");
		btnSearch.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
			}
		});
		btnSearch.setBounds(435, 107, 89, 23);
		contentPane.add(btnSearch);
		
		JButton btnAddSupplier = new JButton("Add supplier");
		btnAddSupplier.setBounds(216, 107, 115, 23);
		contentPane.add(btnAddSupplier);
		
		
		JScrollPane scrollPane = new JScrollPane();
		
		tableProds = new JTable();
		tableProds.setModel(new DefaultTableModel(
			new Object[][] {
			},
			new String[] {
				"ID", "Name", "Sales price", "Country of origin", "Supplier ID", "Size", "Colour"
			}
		) {
			Class[] columnTypes = new Class[] {
				String.class, String.class, Double.class, String.class, String.class, String.class, String.class
			};
			public Class getColumnClass(int columnIndex) {
				return columnTypes[columnIndex];
			}
			boolean[] columnEditables = new boolean[] {
				false, false, false, false, false, false
			};
			public boolean isCellEditable(int row, int column) {
				return columnEditables[column];
			}
		});
		tableProds.getColumnModel().getColumn(0).setResizable(false);
		tableProds.getColumnModel().getColumn(1).setResizable(false);
		tableProds.getColumnModel().getColumn(2).setResizable(false);
		tableProds.getColumnModel().getColumn(3).setResizable(false);
		tableProds.getColumnModel().getColumn(4).setResizable(false);
		tableProds.getColumnModel().getColumn(5).setResizable(false);
		tableProds.getColumnModel().getColumn(6).setResizable(false);
		scrollPane.setViewportView(tableProds);
		
		scrollPane.setBounds(10, 136, 514, 204);
		contentPane.add(scrollPane);
		
		btnAddNew = new JButton("Add new");
		btnAddNew.setBounds(10, 350, 115, 23);
		btnAddNew.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				addprod = new AddProduct("c");
				addprod.setVisible(true);
			}
		});
		contentPane.add(btnAddNew);
		
		btnShowInformation = new JButton("Show information");
		btnShowInformation.setBounds(209, 350, 115, 23);
		btnShowInformation.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				int SelRowIndex = tableProds.getSelectedRow();
				if(SelRowIndex==-1)
				{
					JOptionPane.showMessageDialog(null,
						    "You have to chose one from the list",
						    "Warning",
						    JOptionPane.WARNING_MESSAGE);
				}
				else
				{
					String idSelected = (String) tableProds.getValueAt(SelRowIndex, 0);
					JOptionPane.showMessageDialog(null,
					    ctr.prodInfo(idSelected)); 
				}
			}
		});
		contentPane.add(btnShowInformation);
		
		btnDelete = new JButton("Delete");
		btnDelete.setBounds(409, 351, 115, 23);
		btnDelete.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				int SelRowIndex = tableProds.getSelectedRow();
				if(SelRowIndex==-1)
				{
					JOptionPane.showMessageDialog(null,
						    "You have to chose one from the list",
						    "Warning",
						    JOptionPane.WARNING_MESSAGE);
				}
				else
				{
					String idSelected = (String) tableProds.getValueAt(SelRowIndex, 0);
					ctr.deleteProduct(idSelected);
					JOptionPane.showMessageDialog(null,
							"Deleted!"); 
				}
			}
		});
		contentPane.add(btnDelete);

		btnBackToMain = new JButton("Back to Main Menu");
		btnBackToMain.setBounds(195, 384, 144, 23);
		btnBackToMain.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				MainMenu.getInstance().setVisible(true);
				dispose();
			}
		});
		contentPane.add(btnBackToMain);
	
		
		details();
		
	}
	
	private void details()
	{
		lblSize = new JLabel("Size:");
		lblSize.setBounds(288, 14, 46, 14);
		contentPane.add(lblSize);
		
		comboBox = new JComboBox<String>();
		comboBox.setBounds(324, 11, 46, 20);
		comboBox.setModel(new DefaultComboBoxModel<String>(new String[] {"XS", "S", "M", "L", "XL", "XXL"}));
		contentPane.add(comboBox);
		//from selected the search for given size clothing
		
		lblColour = new JLabel("Colour:");
		lblColour.setBounds(261, 39, 46, 14);
		contentPane.add(lblColour);
		
		txtColour = new JTextField();
		txtColour.setBounds(305, 36, 65, 20);
		contentPane.add(txtColour);
		txtColour.setColumns(10);
		//from entered search for given coloured clothing
		
	}
}
